﻿'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Reflection

Namespace Aspect

    Public Class Verb
        Implements IComparable(Of Verb)
        Friend Sub New(ByVal iVerb As IVerb, ByVal iDelegateInfo As DelegateInfo)
            _Verb = iVerb
            _DelegateInfo = iDelegateInfo
        End Sub
        Private _Verb As IVerb
        Public ReadOnly Property Verb() As IVerb
            Get
                Return _Verb
            End Get
        End Property
        Private _DelegateInfo As DelegateInfo
        Public ReadOnly Property DelegateInfo() As DelegateInfo
            Get
                Return _DelegateInfo
            End Get
        End Property
        Public Overrides Function ToString() As String
            Return Verb.ToString
        End Function
        Public Function CompareTo(ByVal other As Verb) As Integer Implements IComparable(Of Verb).CompareTo
            Return String.Compare(Verb.UIName, other.Verb.UIName)
        End Function

        Public Function Invoke(ByVal params() As Object) As Object
            Dim o As Object = GetAspect(Me.DelegateInfo.DeclaringDelegate)
            Dim m As MethodInfo = o.GetType.GetMethod("Invoke")
            Try
                Return m.Invoke(o, params)
            Catch ex As TargetParameterCountException
                Throw New ArgumentException(String.Format("Verb ""{0}"" was called with the wrong number of parameters.", Me.Verb.UIName), ex)
            End Try
        End Function

    End Class

End Namespace